package com.hm.service.controller.chat;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.service.framework.request.post.PostBase;
import com.hm.service.framework.request.post.PostHandleBean;
import com.hm.service.framework.response.ResponseConstant;
import com.hm.service.pojo.db.UserBean;
import com.hm.service.pojo.response.TokenResponse;
import com.hm.service.service.captcha.ICaptchaService;
import com.hm.service.service.token.ITokenService;
import com.hm.service.service.user.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping(value = "/Chat", produces = {"application/json;charset=UTF-8"})
public class CFSS_000_000_001 extends PostBase {
    private static final Logger logger = LoggerFactory.getLogger(CFSS_000_000_001.class);

    private final IUserService userService;
    private final ICaptchaService captchaService;
    private final ITokenService tokenService;

    public CFSS_000_000_001(IUserService iUserService, ICaptchaService iCaptchaService, ITokenService iTokenService) {
        this.userService = iUserService;
        this.captchaService = iCaptchaService;
        this.tokenService = iTokenService;
    }

    //登录接口
    @PostMapping(value = "/000_000_001")
    public String doPost(HttpServletRequest request) {
        return handleRequest(request);
    }

    @Override
    protected String postRequest(PostHandleBean reqBean) {
        //解析请求参数
        JSONObject jsonObject = (JSONObject) JSON.parse(reqBean.getReqBody());
        String captchaKey = jsonObject.getString("captchaKey");
        String captcha = jsonObject.getString("captcha");
        String userName = jsonObject.getString("userName");
        String userPwd = jsonObject.getString("pwd");

        //非空
        if (captcha == null || captcha.isEmpty()) {
            return errorResponse(ResponseConstant.PARAMETER_ERROR_CODE, "验证码为空！", "");
        }
        if (userName == null || userName.isEmpty() || userPwd == null || userPwd.isEmpty()) {
            return errorResponse(ResponseConstant.PARAMETER_ERROR_CODE, "用户名或密码为空！", "");
        }

        //验证码
        HashMap<String, String> captchaVerify = captchaService.verify(captcha, captchaKey);
        if (!captchaVerify.get("state").equals("0")) {
            return errorResponse(ResponseConstant.PARAMETER_ERROR_CODE, captchaVerify.get("msg"), "");
        }

        //用户
        UserBean userBean = userService.getUserForName(userName);
        if (userBean == null) {
            return errorResponse(ResponseConstant.PARAMETER_ERROR_CODE, "用户不存在！", "");
        }
        String pwd = userBean.userPwd;
        if (!pwd.equals(userPwd)) {
            return errorResponse(ResponseConstant.PARAMETER_ERROR_CODE, "密码错误！", "");
        }
        //完成
        long userId = userBean.userId;
        TokenResponse bean = new TokenResponse();
        bean.token = tokenService.generate(String.valueOf(userId));
        logger.warn("登录成功--》" + userBean.userName);
        return successResponse(reqBean.getRealSecretKey(), ResponseConstant.SUCCESS_CODE, "成功", bean);
    }
}
